/**
 * Copyright 2024 Florida Institute for Human and Machine Cognition (IHMC)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package us.ihmc.pubsub.examples;

import us.ihmc.pubsub.Domain;
import us.ihmc.pubsub.DomainFactory;
import us.ihmc.pubsub.attributes.ParticipantProfile;
import us.ihmc.pubsub.common.Guid;
import us.ihmc.pubsub.common.Time;
import us.ihmc.pubsub.participant.Participant;
import us.ihmc.pubsub.participant.ParticipantDiscoveryInfo;
import us.ihmc.pubsub.participant.ParticipantListener;
import us.ihmc.pubsub.participant.PublisherEndpointDiscoveryListener;
import us.ihmc.pubsub.participant.SubscriberEndpointDiscoveryListener;

import java.io.IOException;

public class EndpointDiscoveryProtocolListenerExample
{
   private class ParticipantListenerImpl implements ParticipantListener
   {
      @Override
      public void onParticipantDiscovery(Participant participant, ParticipantDiscoveryInfo info)
      {
         System.out.println("New participant discovered");
         System.out.println("Status: " + info.getStatus());
         System.out.println("Guid: " + info.getGuid().toString());
         System.out.println("Name: " + info.getName());
      }
   }

   private class PublisherEndpointDiscoveryListenerImpl implements PublisherEndpointDiscoveryListener
   {

      @Override
      public void publisherTopicChange(boolean isAlive,
                                       Guid guid,
                                       Guid participantGuid,
                                       String typeName,
                                       String topicName,
                                       int userDefinedId,
                                       long typeMaxSerialized)
      {
         System.out.println("New publisher topic change");
         System.out.println("Is alive: " + isAlive);
         System.out.println("GUID: " + guid);
         System.out.println("ParticipantGUID: " + participantGuid);
         System.out.println("TypeName: " + typeName);
         System.out.println("TopicName: " + topicName);
      }
   }

   private class SubscriberEndpointDiscoveryListenerImpl implements SubscriberEndpointDiscoveryListener
   {

      @Override
      public void subscriberTopicChange(boolean isAlive,
                                        Guid guid,
                                        boolean expectsInlineQos,
                                        Guid participantGuid,
                                        String typeName,
                                        String topicName,
                                        int userDefinedId)
      {
         System.out.println("New subscriber topic change");
         System.out.println("Is alive: " + isAlive);
         System.out.println("GUID: " + guid);
         System.out.println("ParticipantGUID: " + participantGuid);
         System.out.println("TypeName: " + typeName);
         System.out.println("TopicName: " + topicName);
      }

   }
   
   public EndpointDiscoveryProtocolListenerExample() throws IOException
   {
      Domain domain = DomainFactory.getDomain();

      ParticipantProfile attributes = ParticipantProfile.create()
                                                        .domainId(215)
                                                        .useOnlyIntraProcessDelivery()
                                                        .discoveryLeaseDuration(Time.Infinite)
                                                        .name("EndpointDiscoveryProtocolListenerExample");
      Participant participant = domain.createParticipant(attributes, new ParticipantListenerImpl());
      
      
      participant.registerEndpointDiscoveryListeners(new PublisherEndpointDiscoveryListenerImpl(), new SubscriberEndpointDiscoveryListenerImpl());
      
   }
   
   public static void main(String[] args) throws IOException
   {
      new EndpointDiscoveryProtocolListenerExample();
      
      while(true)
      {
         try
         {
            Thread.sleep(1000);
         }
         catch (InterruptedException e)
         {
         }
      }
   }
}
